package com.gp.library.manager;

import android.content.Context;

import com.gp.library.data.SpKey;
import com.gp.library.utils.StringUtils;
import com.tencent.mmkv.MMKV;

/**
 * Created by jackdeng on 2020/1/3.
 */

public class CommonSPManager {
    private static MMKV mmkv;

    private CommonSPManager() {
    }

    public static void init(Context context) {
        MMKV.initialize(context);
        mmkv = MMKV.defaultMMKV();
    }

    public static void exit() {
        if (checkValid())
            MMKV.onExit();
    }

    private static boolean checkValid() {
        return mmkv != null;
    }

    public static void put(String key, String value) {
        if (checkValid())
            mmkv.encode(key, value);
    }

    public static void put(String key, int value) {
        if (checkValid())
            mmkv.encode(key, value);
    }

    public static void put(String key, boolean value) {
        if (checkValid())
            mmkv.encode(key, value);
    }

    public static void put(String key, float value) {
        if (checkValid())
            mmkv.encode(key, value);
    }

    public static void put(String key, long value) {
        if (checkValid())
            mmkv.encode(key, value);
    }

    public static String get(String key, String defaultValue) {
        if (checkValid())
            return mmkv.decodeString(key, defaultValue);
        else
            return defaultValue;
    }

    public static int get(String key, int defaultValue) {
        if (checkValid())
            return mmkv.decodeInt(key, defaultValue);
        else
            return defaultValue;
    }

    public static boolean get(String key, boolean defaultValue) {
        if (checkValid())
            return mmkv.decodeBool(key, defaultValue);
        else
            return defaultValue;
    }

    public static float get(String key, float defaultValue) {
        if (checkValid())
            return mmkv.decodeFloat(key, defaultValue);
        else
            return defaultValue;
    }

    public static long get(String key, long defaultValue) {
        if (checkValid())
            return mmkv.decodeLong(key, defaultValue);
        else
            return defaultValue;
    }

    public static void saveResId(String num) {
        put(SpKey.RES_ID, num);
    }

    public static String getResIdForPath() {
        String num = getResId();
        if (!StringUtils.isNullOrEmpty(num) && num.equals(getFirstResId()))
            num = "default";
        return num;
    }

    public static String getResId() {
        return get(SpKey.RES_ID, "default");
    }

    public static void saveFirstResId(String num) {
        put(SpKey.FIRST_RES_ID, num);
    }

    public static String getFirstResId() {
        return get(SpKey.FIRST_RES_ID, "");
    }

    public static void saveCookie(String cookie) {
//        put(SpKey.COOKIE + getResIdForPath(), cookie);
        put(SpKey.COOKIE, cookie);
    }

    public static void cleanCookie() {
        saveCookie("");
    }

    public static String getCookie() {
//        return get(SpKey.COOKIE + getResIdForPath(), "");
        return get(SpKey.COOKIE, "");
    }
}
